
import Phaser from "phaser";

const _static = Symbol('_static');
const _error = Symbol('_error');
export class UtilsGroup {
    constructor(_type, _key, _data, _that) {
        if (!_type) return this[_error]('no type?');
        if (!_data) return this[_error]('no data?');
        this.data = _data;
        _type === 'static' ? this[_static](_key, _that) : null;
    }

    [_static](_key, _that) {
        let _arr = [];
        this.data.forEach(element => {
            _arr.push(
                element.setPipe ? _that.physics.add.staticSprite(element.x, element.y, element.key)
                .setData(element.data.key, element.data.data)
                .setPipeline('Light2D') : 
                _that.physics.add.staticSprite(element.x, element.y, element.key)
                .setData(element.data.key, element.data.data)
                // .setOffset(element.off.x, element.off.y)
            );
        })
        _that[_key] =  _that.physics.add.staticGroup(_arr);
    }

    [_error](err) {
        throw err;
    }
}

const _removeItems = Symbol('_removeItems');
export class UtilsGroupChildren extends Phaser.GameObjects.Group {
    constructor(_data, _that) {
        super(_that, _data);
        // set items Data...
        super.getChildren().forEach(element => {
            // console.log(element.texture.key);
            if (element.texture.key === 'clothingEquip') element.setData({id:'02afs2'}).setInteractive();
            if (element.texture.key === 'helmetEquip') element.setData({id:'123faa'}).setInteractive();
            if (element.texture.key === 'pantsEquip') element.setData({id:'457zds'}).setInteractive();

            element.on('pointerdown', () => {
                this[_removeItems](element.data.list.id);
            });
        });
    }

    [_removeItems](_id) {
        super.getChildren().forEach(element => {
            if (element.data.list.id === _id) {
                element.destroy(true, true);
            }
        });
    }
}